﻿Imports Oracle.DataAccess.Client

Public Class TipoPrepack
    Dim drTipoPrepack As dsInventarios.INV_TIPOSPREPACKSRow
    Dim daTipoPrepack As dsInventariosTableAdapters.INV_TIPOSPREPACKSTableAdapter
    Public Sub New(ByVal IdPreapck As Integer)
        daTipoPrepack = New dsInventariosTableAdapters.INV_TIPOSPREPACKSTableAdapter
        Try
            Dim dtTipoPPack As dsInventarios.INV_TIPOSPREPACKSDataTable = daTipoPrepack.GetDataByIdPreack(IdPreapck)
            If dtTipoPPack.Rows.Count > 0 Then
                drTipoPrepack = dtTipoPPack.Rows(0)
            Else
                Throw New Exception("No exite tipo de prepack para este id, favor verificar e intentear de nuevo")
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public ReadOnly Property IdPrepack() As Integer
        Get
            Return drTipoPrepack.IDPREPACK
        End Get
    End Property
    Public ReadOnly Property Descripcion() As String
        Get
            Return drTipoPrepack.DESCRIPCION
        End Get
    End Property
    Public ReadOnly Property CantidadIMEI() As Integer
        Get
            Return drTipoPrepack.CANTIMEI
        End Get
    End Property
    Public ReadOnly Property CantidadICCID() As Integer
        Get
            Return drTipoPrepack.CANTICCID
        End Get
    End Property
End Class
Public Class cPrepack
    Private lDrPPack As dsInventarios.INV_PREPACKRow
    Private lDaPrepack As dsInventariosTableAdapters.INV_PREPACKTableAdapter
    Private lEsPrepack As Boolean
    Public Sub New(ByVal idPrepack As Integer, ByVal consecprepack As Integer)
        lDaPrepack = New dsInventariosTableAdapters.INV_PREPACKTableAdapter
        Try
            Dim DtPrepack As lbGlobalcom.dsInventarios.INV_PREPACKDataTable = lDaPrepack.GetDataByIdPrepackConsec(idPrepack, consecprepack)
            If DtPrepack.Rows.Count > 0 Then
                lDrPPack = DtPrepack.Rows(0)
                lEsPrepack = True
            Else
                lEsPrepack = False
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub New(ByVal TipoSubProd As Integer, ByVal NumeroReg As String, Optional ByVal EsPadre As Boolean = True)
        lDaPrepack = New dsInventariosTableAdapters.INV_PREPACKTableAdapter
        Try
            If EsPadre Then
                Dim DtPrepack As lbGlobalcom.dsInventarios.INV_PREPACKDataTable = lDaPrepack.GetDataByTipoNumeroP(TipoSubProd, NumeroReg)
                If DtPrepack.Rows.Count > 0 Then
                    lDrPPack = DtPrepack.Rows(0)
                    lEsPrepack = True
                Else
                    lEsPrepack = False
                End If
            Else
                Dim DtPrepack As lbGlobalcom.dsInventarios.INV_PREPACKDataTable = lDaPrepack.GetDataByTipoNumeroH(TipoSubProd, NumeroReg)
                If DtPrepack.Rows.Count > 0 Then
                    lDrPPack = DtPrepack.Rows(0)
                    lEsPrepack = True
                Else
                    lEsPrepack = False
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public ReadOnly Property EsPrepack() As Boolean
        Get
            Return lEsPrepack
        End Get
    End Property
    Public ReadOnly Property IdPrepack() As Integer
        Get
            Return lDrPPack.IDPREPACK
        End Get
    End Property
    Public ReadOnly Property ConsecPrepack() As Integer
        Get
            Return cGeneral.NZ(lDrPPack("CONSECPREPACK"))
        End Get
    End Property
    Public ReadOnly Property Costo() As Double
        Get
            Return lDrPPack.COSTO
        End Get
    End Property
    Public ReadOnly Property FechaPrepack() As Date
        Get
            Return lDrPPack.FECHAPREPACK
        End Get
    End Property
    Public ReadOnly Property Estado() As String
        Get
            Return lDrPPack.ESTADO
        End Get
    End Property
    Public ReadOnly Property IdProductoP() As String
        Get
            Return lDrPPack.IDPRODUCTOP
        End Get
    End Property
    Public ReadOnly Property TipoSubProdP() As Int16
        Get
            Return lDrPPack.TIPOSUBPROP
        End Get
    End Property
    Public ReadOnly Property NumeroRegsitroP() As String
        Get
            Return lDrPPack.NUMEROREGISTROP
        End Get
    End Property
    Public ReadOnly Property IdProductoH() As String
        Get
            Return lDrPPack.IDPRODUCTOH
        End Get
    End Property
    Public ReadOnly Property TipoSubProdH() As Int16
        Get
            Return lDrPPack.TIPOSUBPRODH
        End Get
    End Property
    Public ReadOnly Property NumeroRegsitroH() As String
        Get
            Return lDrPPack.NUMEROREGISTROH
        End Get
    End Property
    Public ReadOnly Property Pareja() As String
        Get
            Return lDrPPack.PAREJA
        End Get
    End Property
    Public ReadOnly Property IdTipoPrepack() As Integer
        Get
            Return lDrPPack.IDTIPOPREPACK
        End Get
    End Property
End Class

Public Class cArmaPrepack
    Private lDaPrepack As dsInventariosTableAdapters.INV_PREPACKTableAdapter
    Private lDtPrepack As dsInventarios.INV_PREPACKDataTable
    Private lIdPrepack As Integer
    Public Property pIdPrepack() As Integer
        Get
            Return lIdPrepack
        End Get
        Set(ByVal value As Integer)
            lIdPrepack = value
        End Set
    End Property
    'Private Function GetIdPrepack(ByVal numreg As String)
    '    Dim idP As Integer = GetIdPrepackCreado(numreg)
    '    If idP = 0 And lIdPrepack = 0 Then
    '        Using Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
    '            Using cmd As OracleCommand = Conn.CreateCommand
    '                cmd.CommandText = "SELECT MAX(IDPREPACK) FROM INV_PREPACK"

    '                Try
    '                    Conn.Open()
    '                    lIdPrepack = cGeneral.NZ(cmd.ExecuteScalar)

    '                    'Dim IdPrepack As Integer = cGeneral.NZ(cmd.ExecuteScalar)
    '                    Conn.Close()
    '                    Return lIdPrepack + 1
    '                Catch ex As Exception
    '                    Throw ex
    '                End Try
    '            End Using
    '        End Using
    '    Else
    '        Return lIdPrepack
    '    End If
    'End Function

    'Private Function GetIdPrepackCreado(ByVal numreg As String)
    '    If lIdPrepack = 0 Then
    '        Using Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
    '            Using cmd As OracleCommand = Conn.CreateCommand
    '                cmd.CommandText = "SELECT IDPREPACK FROM INV_PREPACK" & _
    '                                  " WHERE NUMEROREGISTROP=:PNUMREG"

    '                Try
    '                    cmd.Parameters.Add("PNUMREG", OracleDbType.Varchar2).Value = numreg
    '                    Conn.Open()
    '                    lIdPrepack = cGeneral.NZ(cmd.ExecuteScalar)
    '                    Conn.Close()
    '                    Return lIdPrepack
    '                Catch ex As Exception
    '                    Return 0
    '                End Try
    '            End Using
    '        End Using
    '    Else
    '        Return lIdPrepack

    '    End If
    'End Function

    Public Function ExisteEnPrepack(ByVal TIPOSUBPROD As Int16, ByVal NUMEROREGISTRO As String, ByVal EsPadre As Boolean) As Boolean
        Using Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = Conn.CreateCommand
                If EsPadre Then
                    cmd.CommandText = "SELECT COUNT(*) FROM INV_PREPACK WHERE TIPOSUBPROP=:PTIPOSUBPRO AND NUMEROREGISTROP=:PNUMEROREGISTRO "
                Else
                    cmd.CommandText = "SELECT COUNT(*) FROM INV_PREPACK WHERE TIPOSUBPRODH=:PTIPOSUBPRO AND NUMEROREGISTROH=:PNUMEROREGISTRO "
                End If
                cmd.Parameters.Add("PTIPOSUBPRO", OracleDbType.Int16).Value = TIPOSUBPROD
                cmd.Parameters.Add("PNUMEROREGISTRO", OracleDbType.Varchar2).Value = NUMEROREGISTRO
                Try
                    Conn.Open()
                    Dim nReg As Int16 = cmd.ExecuteScalar
                    Conn.Close()
                    If nReg = 0 Then
                        Return False
                    Else
                        Return True
                    End If
                Catch ex As Exception
                    Throw ex
                End Try
            End Using
        End Using
    End Function

    Private Sub InsertEnPrepack(ByVal CONSECPADRE As Integer, ByVal IDPRODUCTOP As String, ByVal TIPOSUBPRODP As Int16, ByVal NUMREGP As String, ByVal COSTOP As Double, ByVal IDPRODUCTOH As String, ByVal TIPOSUBPRODH As Int16, ByVal NUMREGH As String, ByVal COSTOH As Double, ByVal PAREJA As String, ByVal IDTIPOPREPACK As Integer, ByVal IDPREPACK As Integer)
        Using Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = Conn.CreateCommand
                cmd.CommandText = "INV_INSERTPREPACK"
                cmd.CommandType = CommandType.StoredProcedure
                'Dim prepack As Integer = GetIdPrepack(NUMREGP)
                cmd.Parameters.Add("PIDPREPACK", OracleDbType.Int32).Value = IDPREPACK
                cmd.Parameters.Add("PCONSECPREPACK", OracleDbType.Int16).Value = CONSECPADRE
                cmd.Parameters.Add("PCOSTO", OracleDbType.Decimal).Value = COSTOH + COSTOP
                cmd.Parameters.Add("PFECHAPREPACK", OracleDbType.Date).Value = Date.Today
                cmd.Parameters.Add("PESTADO", OracleDbType.Varchar2).Value = "S"
                cmd.Parameters.Add("PIDPRODUCTOP", OracleDbType.Varchar2).Value = IDPRODUCTOP
                cmd.Parameters.Add("PTIPOSUBPROP", OracleDbType.Int16).Value = TIPOSUBPRODP
                cmd.Parameters.Add("PNUMEROREGISTROP", OracleDbType.Varchar2).Value = NUMREGP
                cmd.Parameters.Add("PIDPRODUCTOH", OracleDbType.Varchar2).Value = IDPRODUCTOH
                cmd.Parameters.Add("PTIPOSUBPROH", OracleDbType.Int16).Value = TIPOSUBPRODH
                cmd.Parameters.Add("PNUMEROREGISTROH", OracleDbType.Varchar2).Value = NUMREGH
                cmd.Parameters.Add("PPAREJA", OracleDbType.Varchar2).Value = PAREJA
                cmd.Parameters.Add("PIDTIPOPREPACK", OracleDbType.Int16).Value = IDTIPOPREPACK
                Try
                    Conn.Open()
                    cmd.ExecuteNonQuery()
                    Conn.Close()
                    'GeneraPrepakDesdeMovimi()
                Catch ex As Exception
                    Throw ex
                End Try
            End Using
        End Using
    End Sub

    Public Sub New(ByVal prep As Integer)
        lIdPrepack = prep
    End Sub

    Public Sub New(ByVal CONSECPADRE As Integer, ByVal IDPRODUCTOP As String, ByVal TIPOSUBPRODP As Int16, ByVal NUMREGP As String, ByVal COSTOP As Double, ByVal IDPRODUCTOH As String, ByVal TIPOSUBPRODH As Int16, ByVal NUMREGH As String, ByVal COSTOH As Double, ByVal PAREJA As String, ByVal IDTIPOPREPACK As Int16, ByVal IDPREPACK As Integer)
        If Not ExisteEnPrepack(TIPOSUBPRODP, NUMREGP, True) Then
            If Not ExisteEnPrepack(TIPOSUBPRODH, NUMREGH, False) Then
                InsertEnPrepack(CONSECPADRE, IDPRODUCTOP, TIPOSUBPRODP, NUMREGP, COSTOP, IDPRODUCTOH, TIPOSUBPRODH, NUMREGH, COSTOH, PAREJA, IDTIPOPREPACK, IDPREPACK)
            Else
                Throw New Exception("Ya existe registro para " & NUMREGH & ", como hijo en un prepack")
            End If
        Else
            Throw New Exception("Ya existe registro para " & NUMREGP & ", como padre en un prepack")
        End If
    End Sub
    Public Sub New(ByVal CONSECPADRE As Integer, ByVal IDPRODUCTOP As String, ByVal TIPOSUBPRODP As Int16, ByVal NUMREGP As String, ByVal COSTOP As Double, ByVal IDPRODUCTOH As String, ByVal TIPOSUBPRODH As Int16, ByVal NUMREGH As String, ByVal COSTOH As Double, ByVal tipo As Integer, ByVal PAREJA As String, ByVal IDTIPOPREPACK As Int16, ByVal IDPREPACK As Integer)
        If Not ExisteEnPrepack(TIPOSUBPRODP, NUMREGP, True) Then
            If Not ExisteEnPrepack(TIPOSUBPRODH, NUMREGH, False) Then
                InsertEnPrepack(CONSECPADRE, IDPRODUCTOP, TIPOSUBPRODP, NUMREGP, COSTOP, IDPRODUCTOH, TIPOSUBPRODH, NUMREGH, COSTOH, PAREJA, IDTIPOPREPACK, IDPREPACK)
            Else
                Throw New Exception("Ya existe registro para " & NUMREGH & ", como hijo en un prepack")
            End If
        Else
            Throw New Exception("Ya existe registro para " & NUMREGP & ", como padre en un prepack")
        End If
    End Sub
End Class

Public Class cEliminaPrepaks
    Private lNumeroPrepacks As Integer = 0
    Private lNumeroPreakcsVendidos As Integer = 0
    Private lPreapcksVendidos As System.Text.StringBuilder
    Public Sub EliminaPrepack(ByVal IdPreapck As Integer, ByVal ConsecPrepack As Integer)
        Using Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = Conn.CreateCommand
                cmd.CommandText = "INV_ELIMINAPREPACK"
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("PIDPREPACK", OracleDbType.Int64).Value = IdPreapck
                cmd.Parameters.Add("PCONSECPREPACK", OracleDbType.Int64).Value = ConsecPrepack
                Try
                    Conn.Open()
                    cmd.ExecuteNonQuery()
                    Conn.Close()
                Catch ex As Exception
                    Throw ex
                End Try
            End Using
        End Using
    End Sub
    Public Sub New()
    End Sub
    Public Sub New(ByVal TipoSubProd As Integer, ByVal NumeroRestro As String, ByVal EsPadre As Boolean)
        Dim PreP As cPrepack = New cPrepack(TipoSubProd, NumeroRestro, EsPadre)
        If PreP.Estado = "V" Then
            lNumeroPreakcsVendidos += 1
            lPreapcksVendidos.Append("Padre: " & PreP.NumeroRegsitroP & " - Hijo:" & PreP.NumeroRegsitroH & vbNewLine)
            Throw New Exception("Este prepack ya fue facturado con numero registro padre " & PreP.NumeroRegsitroP & ", y numero registro hijo " & PreP.NumeroRegsitroH)
        Else
            EliminaPrepack(PreP.IdPrepack, PreP.ConsecPrepack)
            lNumeroPrepacks += 1
        End If
    End Sub
    Public Sub New(ByVal Padre As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow())
        Try
            lNumeroPrepacks = 0
            lNumeroPreakcsVendidos = 0
            lPreapcksVendidos = New System.Text.StringBuilder
            For Each drPadre As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow In Padre
                Dim PreP As cPrepack = New cPrepack(drPadre.TIPOSUBPROD, drPadre.NUMEROREGISTRO, True)
                If PreP.Estado = "V" Then
                    lNumeroPreakcsVendidos += 1
                    lPreapcksVendidos.Append("Padre: " & PreP.NumeroRegsitroP & " - Hijo:" & PreP.NumeroRegsitroH & vbNewLine)
                    Throw New Exception("Este prepack ya fue facturado con numero registro padre " & PreP.NumeroRegsitroP & ", y numero registro hijo " & PreP.NumeroRegsitroH)
                Else
                    EliminaPrepack(PreP.IdPrepack, PreP.ConsecPrepack)
                    lNumeroPrepacks += 1
                End If
            Next
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public ReadOnly Property PreapksVendidos() As String
        Get
            Return lPreapcksVendidos.ToString
        End Get
    End Property
    Public ReadOnly Property NumeroPrepakcsVendidos() As Integer
        Get
            Return lNumeroPreakcsVendidos
        End Get
    End Property
    Public ReadOnly Property NumeroPreacksEliminados() As Integer
        Get
            Return lNumeroPrepacks
        End Get
    End Property
End Class

Public Class GeneraPrepakDesdeMovimi
    Private lPadre As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow()
    Private lHijo As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow()
    Private dtPadre As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETDataTable
    Private dtHijo As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETDataTable
    Private lCantidadIccid As Integer
    Private lNumeroRegistrosIguales As Boolean
    Private lParesIguales As Boolean
    Private lCodigoP As String
    Private lTipoSubProdP As Int16
    Private lCodigoH As String
    Private lTipoSubProdH As Int16
    Private lNumeroPrepacks As Integer = 0
    Private lNumerosPrepackExistentes As Integer = 0
    Private lIdTipoPrepack As Integer
    Private lIdPrepack As Integer
    Public Sub ValidaInformacion()
        Dim nRegPadre As Integer = lPadre.Count
        Dim nRegHijo As Integer = lHijo.Count
        If nRegPadre = nRegHijo Then
            lNumeroRegistrosIguales = True
        Else
            lNumeroRegistrosIguales = False
        End If
        For i = 0 To cGeneral.GetMenor(nRegPadre, nRegHijo) - 1
            Dim drPadre As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow = lPadre.ElementAt(i)
            Dim drHijo As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow = lHijo.ElementAt(i)
            lParesIguales = True
            If drPadre.NUMEROPRODUCTO <> drHijo.NUMEROPRODUCTO Then
                lParesIguales = False
                Exit For
            End If
        Next
    End Sub
    Public ReadOnly Property NumeroRegIguales() As Boolean
        Get
            Return lNumeroRegistrosIguales
        End Get
    End Property
    Public ReadOnly Property ParesIguales() As Boolean
        Get
            Return lParesIguales
        End Get
    End Property
    Public ReadOnly Property NumeroPrepacksArmados() As Integer
        Get
            Return lNumeroPrepacks
        End Get
    End Property
    Public ReadOnly Property PrepacksExistentes() As Integer
        Get
            Return lNumerosPrepackExistentes
        End Get
    End Property
    Public Sub New(ByVal Padre As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow(), ByVal Hijo As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow(), ByVal IdTipoPrepack As Integer)
        lIdTipoPrepack = IdTipoPrepack
        lPadre = Padre
        lHijo = Hijo
        If lPadre.Count > 0 Then
            lCodigoP = lPadre.ElementAt(0)("IDPRODUCTO")
            lTipoSubProdP = lPadre.ElementAt(0)("TIPOSUBPROD")
        End If
        If lHijo.Count > 0 Then
            lCodigoH = lHijo.ElementAt(0)("IDPRODUCTO")
            lTipoSubProdH = lHijo.ElementAt(0)("TIPOSUBPROD")
        End If
        ValidaInformacion()
    End Sub

    Public Sub New(ByVal dtP As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETDataTable, ByVal dtH As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETDataTable, ByVal cantIccid As Integer, ByVal tipoPre As Integer)
        dtPadre = dtP
        dtHijo = dtH
        lCantidadIccid = cantIccid
        lIdTipoPrepack = tipoPre
    End Sub

    Private Function GetRegHijo(ByVal NumeroProdPadre As Integer) As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow
        For Each drHijo As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow In lHijo
            If drHijo.NUMEROPRODUCTO = NumeroProdPadre Then
                Return drHijo
            End If
        Next
        Return Nothing
    End Function

    Public Function validaNumeroProducto() As Integer
        Dim numProPadre As Integer
        Dim numProHijo As Integer
        Try
            For i As Int16 = 0 To dtPadre.Count - 1
                numProPadre = dtPadre(i).NUMEROPRODUCTO
            Next

            For i As Int16 = 0 To dtHijo.Count - 1
                numProHijo = dtHijo(i).NUMEROPRODUCTO
            Next

            If numProPadre = numProHijo Then
                Return 1
            Else
                Return 0
            End If
        Catch ex As Exception

        End Try
    End Function

    Private Function GetIdPrepack(ByVal numreg As String)
        Dim idP As Integer = GetIdPrepackCreado(numreg)
        If idP = 0 And lIdPrepack = 0 Then
            Using Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
                Using cmd As OracleCommand = Conn.CreateCommand
                    cmd.CommandText = "SELECT MAX(IDPREPACK) FROM INV_PREPACK"

                    Try
                        Conn.Open()
                        lIdPrepack = cGeneral.NZ(cmd.ExecuteScalar)

                        'Dim IdPrepack As Integer = cGeneral.NZ(cmd.ExecuteScalar)
                        Conn.Close()
                        lIdPrepack += 1
                        Return lIdPrepack
                    Catch ex As Exception
                        Throw ex
                    End Try
                End Using
            End Using
        Else
            Return lIdPrepack
        End If
    End Function

    Private Function GetIdPrepackCreado(ByVal numreg As String)
        If lIdPrepack = 0 Then
            Using Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
                Using cmd As OracleCommand = Conn.CreateCommand
                    cmd.CommandText = "SELECT IDPREPACK FROM INV_PREPACK" & _
                                      " WHERE NUMEROREGISTROP=:PNUMREG"

                    Try
                        cmd.Parameters.Add("PNUMREG", OracleDbType.Varchar2).Value = numreg
                        Conn.Open()
                        lIdPrepack = cGeneral.NZ(cmd.ExecuteScalar)
                        Conn.Close()
                        Return lIdPrepack
                    Catch ex As Exception
                        Return 0
                    End Try
                End Using
            End Using
        Else
            Return lIdPrepack

        End If
    End Function

    Public Sub GeneraPrepackDesdeFormulario()
        Try
            lNumeroPrepacks = 0
            Dim drPadre As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow
            Dim drHijo As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow
            Dim j As Integer = 0
            Dim iccid As Integer = 0
            Dim nuevo As Integer = 0

            For i As Int16 = 0 To dtPadre.Count - 1
                drPadre = dtPadre.Rows(i)
                If j = 0 Then
                    drHijo = dtHijo.Rows(j)
                    nuevo = 1
                ElseIf (drPadre("NUMEROPRODUCTO") <> drHijo("NUMEROPRODUCTO")) Or iccid < lCantidadIccid Then
                    drHijo = dtHijo.Rows(j)
                    nuevo = 1
                    iccid = 0
                End If
                Try
                    If drPadre("NUMEROPRODUCTO") = drHijo("NUMEROPRODUCTO") And nuevo = 1 Then

                        Dim ArmaPrep As New cArmaPrepack(drPadre("NUMEROPRODUCTO"), drPadre("IDPRODUCTO"), drPadre("TIPOSUBPROD"), drPadre("NUMEROREGISTRO"), cGeneral.NZ(drPadre("COSTO")), drHijo("IDPRODUCTO"), drHijo("TIPOSUBPROD"), drHijo("NUMEROREGISTRO"), cGeneral.NZ(drHijo("COSTO")), lIdPrepack, getPareja(drPadre("NUMEROREGISTRO")), lIdTipoPrepack, GetIdPrepack(drPadre("NUMEROREGISTRO")))
                        'ArmaPrep.pIdPrepack = lIdPrepack
                        ArmaPrep = Nothing
                        nuevo = 0
                        lNumeroPrepacks += 1
                        j += 1
                        iccid += 1
                    ElseIf nuevo = 0 Then
                        Dim ArmaPrep As New cArmaPrepack(drPadre("NUMEROPRODUCTO"), drPadre("IDPRODUCTO"), drPadre("TIPOSUBPROD"), drPadre("NUMEROREGISTRO"), cGeneral.NZ(drPadre("COSTO")), "", 0, "", 0, 0, getPareja(drPadre("NUMEROREGISTRO")), lIdTipoPrepack, GetIdPrepack(drPadre("NUMEROREGISTRO")))
                        'ArmaPrep.pIdPrepack = lIdPrepack
                        ArmaPrep = Nothing
                    End If
                Catch ex As Exception
                    If ex.Message.IndexOf("Ya existe registro para") <> -1 Then
                        lNumerosPrepackExistentes += 1
                        If drPadre("NUMEROPRODUCTO") = drHijo("NUMEROPRODUCTO") And nuevo = 1 Then
                            nuevo = 0
                            'lNumeroPrepacks += 1
                            j += 1
                            iccid += 1
                        End If
                    Else
                        Throw ex
                    End If
                End Try
            Next
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Function getPareja(ByVal numeroRegistro As String) As String
        Using Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = Conn.CreateCommand
                cmd.CommandText = "SELECT PAREJA" & _
                                  " FROM INV_DETALLE" & _
                                  " WHERE NUMEROREGISTRO=:PNUMREG"
                cmd.Parameters.Add("PNUMREG", OracleDbType.Varchar2).Value = numeroRegistro
                Try
                    Conn.Open()
                    Return cmd.ExecuteScalar
                Catch ex As Exception

                    Throw ex
                Finally
                    Conn.Close()
                End Try
            End Using
        End Using
    End Function

    Public Sub GeneraPrepacksCoincidentes()
        Try
            lNumeroPrepacks = 0
            lNumerosPrepackExistentes = 0
            For i As Int16 = 0 To lPadre.Count - 1
                Dim drPadre As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow = lPadre(i)
                Dim drHijo As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow = lHijo(i)
                Try
                    Dim ArmaPrep As New cArmaPrepack(drPadre.NUMEROPRODUCTO, drPadre.IDPRODUCTO, drPadre.TIPOSUBPROD, drPadre.NUMEROREGISTRO, cGeneral.NZ(drPadre("COSTO")), drHijo.IDPRODUCTO, drHijo.TIPOSUBPROD, drHijo.NUMEROREGISTRO, cGeneral.NZ(drHijo("COSTO")), drPadre("IDDOCUMENTO").ToString.Trim & drPadre("NUMERO"), lIdTipoPrepack, GetIdPrepack(drPadre.NUMEROREGISTRO))
                    ArmaPrep = Nothing
                    lNumeroPrepacks += 1
                Catch ex As Exception
                    If ex.Message.IndexOf("Ya existe registro para") <> -1 Then
                        lNumerosPrepackExistentes += 1
                    Else
                        Throw ex
                    End If
                End Try
            Next
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub GeneraPrepacKsNoCoincidentes()
        Try
            lNumeroPrepacks = 0
            For Each drPadre As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow In lPadre
                Dim drHijo As lbGlobalcom.dsMovimiento.MOV_DESCRIPCIONDETRow = GetRegHijo(drPadre.NUMEROPRODUCTO)
                If drHijo IsNot Nothing Then
                    Dim ArmaPrep As New cArmaPrepack(drPadre.NUMEROPRODUCTO, drPadre.IDPRODUCTO, drPadre.TIPOSUBPROD, drPadre.NUMEROREGISTRO, cGeneral.NZ(drPadre("COSTO")), drHijo.IDPRODUCTO, drHijo.TIPOSUBPROD, drHijo.NUMEROREGISTRO, drHijo.COSTO, drPadre("IDDOCUMENTO").ToString.Trim & drPadre("NUMERO"), lIdTipoPrepack, GetIdPrepack(drPadre.NUMEROREGISTRO))
                    ArmaPrep = Nothing
                    lNumeroPrepacks += 1
                End If
            Next
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
End Class
Public Class GetDatosPrepack
    Private lDtDPPack As dsInvenConsultas.INV_GETPREPACKDataTable
    Private lEsPPack As Boolean = False
    Private lEsPadre As Boolean = False
    Private iTipoPPack As TipoPrepack
    Private lEstadoPrepack As String
    Private Function EsPadre(ByVal IdProducto As String) As Boolean
        Try
            Dim cProd As cProducto = New cProducto(IdProducto)
            If cProd.EsPadre.ToUpper = "S" Then
                lEsPadre = True
                Return True
            Else
                lEsPadre = False
                Return False
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public ReadOnly Property Prepack() As dsInvenConsultas.INV_GETPREPACKDataTable
        Get
            Return lDtDPPack
        End Get
    End Property
    Public ReadOnly Property EstadoPrepack() As String
        Get
            Return lEstadoPrepack
        End Get
    End Property
    Public ReadOnly Property PPackEsPadre() As Boolean
        Get
            Return lEsPadre
        End Get
    End Property
    Private Function GetDatosPrepack(ByVal idPrepack As Integer, ByVal ConsecPrepack As Integer) As dsInvenConsultas.INV_GETPREPACKDataTable
        Using conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
            Using cmd As OracleCommand = conn.CreateCommand
                cmd.CommandText = "SELECT * FROM INV_PREPACK WHERE IDPREPACK=:PIDPREPACK AND CONSECPREPACK=:PCONSECPREPACK"
                cmd.Parameters.Add("PIDPREPACK", OracleDbType.Int32).Value = idPrepack
                cmd.Parameters.Add("PCONSECPREPACK", OracleDbType.Int32).Value = ConsecPrepack
                Try
                    conn.Open()
                    Dim dt As New dsInvenConsultas.INV_GETPREPACKDataTable
                    dt.Load(cmd.ExecuteReader)
                    conn.Clone()
                    Return dt
                Catch ex As Exception
                    Throw ex
                End Try
            End Using
        End Using
    End Function
    Public Sub New(ByVal IdProducto As String, ByVal TipoSubProd As Int16, ByVal NumeroRegistro As String)
        Try
            Dim cPPack As New cPrepack(TipoSubProd, NumeroRegistro, EsPadre(IdProducto))
            If cPPack.EsPrepack Then
                iTipoPPack = New TipoPrepack(cPPack.IdTipoPrepack)
                lDtDPPack = GetDatosPrepack(cPPack.IdPrepack, cPPack.ConsecPrepack)
                lEsPPack = True
                Dim drPrepack As dsInvenConsultas.INV_GETPREPACKRow = lDtDPPack.Rows(0)
                lEstadoPrepack = drPrepack.ESTADO
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public ReadOnly Property TipoPPack() As TipoPrepack
        Get
            Return iTipoPPack
        End Get
    End Property
    Public ReadOnly Property EsPrepack() As Boolean
        Get
            Return lEsPPack
        End Get
    End Property
End Class